package com.young.bs.vote.service.impl;

import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.bs.vote.model.VoteResult;
import com.young.bs.vote.service.IVoteResultService;
import com.young.common.util.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* 投票结果服务实现
* Created by admin on 2019-3-19.
*/
@Service("voteResultService")
public class VoteResultServiceImpl implements IVoteResultService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(VoteResultServiceImpl.class);

    private static final String MAPPER = "com.young.bs.vote.mapper.voteResult";//mapper的namespace

    //获取数据列表
    @Override
    public List<VoteResult> getList(VoteResult obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<VoteResult> getPage(VoteResult obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public VoteResult get(String id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    /**
     * 根据票id和用户id查询结果信息
     *
     * @param voteId
     * @param userId
     * @return
     */
    @Override
    public VoteResult getByVoteIdAndUserId(String voteId, String userId) {
        VoteResult voteResult = new VoteResult();
        voteResult.setVoteId(voteId);
        voteResult.setVoterId(userId);
        return dataAccessService.getObject(MAPPER + ".getByVoteIdAndUserId", voteResult);
    }

    //统计数量
    @Override
    public int count(VoteResult obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(VoteResult obj) {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(VoteResult obj) {
        if (StringUtils.isBlank(obj.getId())) {
            obj.setId(CommonUtil.getUUID());
        }
        if (obj.getVoteTime() == null) {
            obj.setVoteTime(new Date());
        }
        if (obj.getState() == null) {
            obj.setState(1);
        }
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<VoteResult> list) {
        int i = 0;
        for(VoteResult item : list){
            i += this.insert(item);
        }
        return i;
    }

}